import os.path
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

# 导包路径
"""
os.path.abspath(__file__)  当前文件的绝对路径
os.path.abspath('html')  html的绝对路径
本地文件前面都有固定前缀[file:///],需要进行字符串拼接
"""


class MouseOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    # 按下鼠标左键在一个元素上
    def click_and_hold(self):
        # 请求本地页面
        file = 'file:///' + os.path.abspath('html/mouse_hold.html')
        self.driver.get(file)
        time.sleep(3)
        ele = self.driver.find_element(By.ID, 'btn1')
        ac = ActionChains(self.driver)
        ac.click_and_hold(ele).perform()
        time.sleep(5)

    def quit(self):
        self.driver.quit()


if __name__ == '__main__':
    mo = MouseOperation()
    mo.click_and_hold()
    mo.quit()
